package com.rzl.expense.quartz.task;


import com.rzl.expense.agent.DataAgentService;
import com.rzl.expense.common.utils.DateUtils;
import com.rzl.expense.expense.domain.ElePayment;
import com.rzl.expense.expense.domain.TwrTowerBillBalance;
import com.rzl.expense.expense.domain.TwrTowerBillBalanceSnapShoot;
import com.rzl.expense.expense.service.ITwrTowerBillBalanceService;
import com.rzl.expense.expense.service.ITwrTowerBillBalanceSnapShootService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 铁塔室分传输微站非标月报快照
 *
 * @author admin
 * @date 2023/08/09
 */
@Slf4j
@Component("TwrTowerBillBalanceShootTask")  // 2.开启定时任务
public class TwrTowerBillBalanceShootTask {
    @Autowired
    private DataAgentService dataAgentService;

    @Autowired
    private ITwrTowerBillBalanceSnapShootService billBalanceService;


    public void run(String startMonth,String endMonth) {
        log.info("获取集团用户数据..........");
        // 执行sql

        List<String> tabDate = DateUtils.getMonths(startMonth,endMonth);
        for (String date : tabDate) {
            int size;
            // limit起始位置
            int startIndex = 0;
            // limit大小
            int limit = 20000;
            String accountPeroid=date.replace("-","");
            billBalanceService.deleteTwrTowerBillBalanceSnapShootByDate(accountPeroid);
            do {

                List<TwrTowerBillBalanceSnapShoot> list = (List<TwrTowerBillBalanceSnapShoot>) dataAgentService.getData(RuleSqlAdapter.getTowerBillBalanceShoot(accountPeroid, startIndex, limit), TwrTowerBillBalanceSnapShoot.class);
                if (!Objects.isNull(list) && list.size() > 0) {
                    int insertSize = list.size();
                    // limit起始位置
                    int insertIndex = 0;
                    // limit大小
                    do {
                        List<TwrTowerBillBalanceSnapShoot> paymentResourceDetails = list.stream().skip(insertIndex).limit(1000).collect(Collectors.toList());
                        try {
                            if (!Objects.isNull(paymentResourceDetails) && paymentResourceDetails.size() > 0) {
                                billBalanceService.batchTwrTowerBillBalanceSnapShoot(paymentResourceDetails);
                                insertSize = paymentResourceDetails.size();
                                insertIndex = insertIndex + 1000;
                            } else {
                                insertSize = 0;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            System.out.println(paymentResourceDetails);
                        }
                    } while (insertSize >= 1000);
                }
                size = list.size();
                startIndex = startIndex + limit;
            } while (size >= limit);
        }
        log.info("数据操作成功");
    }
}
